package br.com.sms.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import br.com.sms.dominio.Destinatario;
import br.com.sms.dto.EtiquetaDTO;
import br.com.sms.enuns.UF;
import br.com.sms.facade.DestinatarioFacade;



public class Relatorio extends HttpServlet{
	static final long serialVersionUID = 1L;
	
	/**
	 * @descricao gera um relatorio de acordo com os parametros passados
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletOutputStream servletOutputStream = response.getOutputStream();
		  
		EtiquetaDTO dto = gerarEtiquetas(request, response);
		HashMap<String, Object> map = getMapaParametros(dto);

		String path = getServletConfig().getServletContext().getRealPath("WEB-INF/relatorios/");
		path += "/" + map.get("relatorio");
		
		try {
			JasperPrint fillReport = JasperFillManager.fillReport(path, map, new JREmptyDataSource());  
			byte[] bytes = JasperExportManager.exportReportToPdf(fillReport);
			
			// envia o relatório em formato PDF para o browser
			response.setContentType("application/pdf");
			servletOutputStream.write(bytes, 0, bytes.length);  
			servletOutputStream.flush();
			servletOutputStream.close();
		}
		catch (JRException e) {
			// display stack trace in the browser
			StringWriter stringWriter = new StringWriter();
			PrintWriter printWriter = new PrintWriter(stringWriter);
			e.printStackTrace(printWriter);
			response.setContentType("text/plain");
			response.getOutputStream().print(stringWriter.toString());
		}
	} 

	public EtiquetaDTO gerarEtiquetas(HttpServletRequest request, HttpServletResponse response){
		String uf = request.getParameter("uf");
		DestinatarioFacade facade = new DestinatarioFacade();
		UF uf2 =  null;
		if(uf != null && uf.trim().length() > 0){
			uf2 = UF.newInstance(uf);
		}
		List<Destinatario> dest  = facade.recuperarDestinatarios(uf2);
		EtiquetaDTO etiquetaDTO = new EtiquetaDTO();
		etiquetaDTO.setDestinatarios(dest);
		return etiquetaDTO;
	}


	protected HashMap<String, Object> getMapaParametros(EtiquetaDTO etiquetaDTO){
		HashMap<String, Object> parameterMap;
		parameterMap = new HashMap<String, Object>();

		try {
			Collection<Destinatario> listaDestinatarios = etiquetaDTO.getDestinatarios();

			JRBeanCollectionDataSource jrLista = new JRBeanCollectionDataSource(listaDestinatarios);
			
			parameterMap.put("listaDestinatarios", jrLista);						
			parameterMap.put("relatorio", "etiquetas.jasper");
			parameterMap.put("nuDemanda", listaDestinatarios.iterator().next().getNome());
			parameterMap.put("CAMINHO_JASPERS", "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return parameterMap;
	}


	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}  	

	/* (non-Java-doc)relatorioUsuario
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}